package com.xiaoxu.networktest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private static final int SHOW_RESPONE = 0;
	private Button send;
	private TextView responseText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		send = (Button)findViewById(R.id.send_request);
		responseText = (TextView)findViewById(R.id.response_text);
		
		send.setOnClickListener(this);
		responseText.setOnClickListener(this);
	
	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case SHOW_RESPONE:
				
				String responce = (String)msg.obj;
				responseText.setText(responce);
				
				break;
			 
			default:
				break;
			}
		};
	};
	

	@Override
	public void onClick(View v) {
		
		if(v.getId() == R.id.send_request){
			
			//sendRequestWithHttpURLConnection();
			
			sendRequestWithHttpClient();
		}
		
		
	}

	private void sendRequestWithHttpClient() {
		// TODO Auto-generated method stub
	
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub

				HttpClient client = new DefaultHttpClient();
				
				HttpGet httpGet = new HttpGet("http://www.baidu.com");
				
				
				try {
					HttpResponse httpRespnese =  client.execute(httpGet);
						
					if(httpRespnese.getStatusLine().getStatusCode() == 200){
							
						HttpEntity entity = httpRespnese.getEntity();
						
						String response = EntityUtils.toString(entity,"utf-8");
						
						Message message = new Message();
						
						message.what = SHOW_RESPONE;
						message.obj = response.toString();
						
						handler.sendMessage(message);
						
						}
					
					
					
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}).start();
		
		
	}

	
	
	
	
	private void sendRequestWithHttpURLConnection() {
		// TODO Auto-generated method stub
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
	
				HttpURLConnection connection = null;
				
				try {
					
					URL url = new URL("http://www.baidu.com");
					
					connection = (HttpURLConnection) url.openConnection();
					
					connection.setRequestMethod("GET");
					
					connection.setReadTimeout(8000);
					
					connection.setReadTimeout(8000);
					
					InputStream in = connection.getInputStream();
					
					BufferedReader br = new BufferedReader(new InputStreamReader(in));
					String Line;
					StringBuilder response = new StringBuilder();
					
					while((Line = br.readLine())!= null){
						
						response.append(Line);
						
					}
					
					Message message = new Message();
					message.what = SHOW_RESPONE;
					
					message.obj = response.toString();
					
					handler.sendMessage(message);
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		}).start();
		
	}
}
